package com.amoyhong.hongWhether.utils;

import android.location.Address;
import android.location.Geocoder;

import com.amoyhong.hongWhether.MyApplication;
import com.orhanobut.logger.Logger;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2016/4/4.
 */
public class GeoLocationHelper {

    public static String getLocationName(double lattitude, double longitude) {

        String cityName = "";
        Geocoder gcd = new Geocoder(MyApplication.getInstance(), Locale.getDefault());
        try {

            List<Address> addresses = gcd.getFromLocation(lattitude, longitude, 10);

            for (Address adrs : addresses) {
                if (adrs != null) {

                    String city = adrs.getLocality();
                    if (city != null && !city.equals("")) {
                        cityName = city;
                        Logger.d("city ::  " + cityName);
                    } else {

                    }
                    // // you should also try with addresses.get(0).toSring();

                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cityName;

    }

    public static class NoFoundLocationException extends Exception {
        public NoFoundLocationException() {
            super(" location name not found");
        }
    }

}
